import redisClient from 'redis';
import config from '../config/config';
const client = redisClient.createClient(config.redis);
const client1 = redisClient.createClient(config.redis);


class redis {
    static redisConfig = config.redis;
    constructor() { }
    /**
     * 写入字符串
     * @param key 唯一标识
     * @param val 存入的值
     * @param time 存在时间
     */
    static setString(key: string, val: string, time?: number): void {
        client.set(key, val);
        if (time) {
            client.expire(key, time);
        }
    }

    /**
     * 读取字符串
     * @param key 唯一标识
     */
    static getString(key: string): Promise<string | null> {
        return new Promise((resolve, reject) => {
            client.get(key, (err, data) => {
                if (err) reject(err);
                resolve(data)
            })
        })
    }

    /**
     * 删除字符串
     * @param key 唯一标识
     */
    static delString(key: string): boolean {
        return client.del(key);
    }

    /**
     * 判断键是否存在
     * @param key 唯一标识
     */
    static isExist(key: string): boolean {
        return client.exists(key);
    }

    /**
     * hash数据类型
     * @param key 唯一标识
     * @param k 参数key
     * @param v 参数值
     */
    static hset(key: string, k: string, v: string, time?: number) {
        client.hset(key, k, v);
        if (time) {
            client.hset(key, k, v);
        }
    }

    /**
     * 获取hash数据类型
     * @param key 唯一标识
     * @param k 参数key
     */
    static hget(key: string, k: string) {
        return new Promise((resolve, reject) => {
            client.hget(key, k, (err, data) => {
                if (err) reject(err);
                resolve(data)
            })
        })
    }

    /**
     * 写入多个hash数据类型
     * @param key 唯一标识
     * @param json json对象
     */
    static hmset(key: string, json: any) {
        client.hmset(key, json);
    }

    /**
     * 获取全部hash数据类型
     * @param key 唯一标识
     */
    static hgetall(key: string) {
        return new Promise((resolve, reject) => {
            client.hgetall(key, (err, data) => {
                if (err) reject(err);
                resolve(data)
            })
        })
    }

    /**
     * 自动增加数量
     * @param key 唯一标识
     * @param val 增加的数量
     */
    static incrby(key: string, val: number) {
        client.incrby(key, val);
    }

    /**
     * 自动减少数量
     * @param key 唯一标识
     * @param val 减少的数量
     */
    static decrby(key: string, val: number) {
        client.decrby(key, val);
    }

    /**
     * 移除过期时间
     * @param key 唯一标识
     */
    static persist(key: string) {
        client.persist(key);
    }
    /**
     * 清空库
     * @param db 需要清空的库
     */
    static flushdb(db: number = 0) {
        client.flushdb();
    }

    static publish(channel: string, message: any) {
        client.publish(channel, message);
        redis.msgOn();
    }

    /**
     * 订阅
     * @param channel 频道
     */
    static subscribe(channel: string) {
        client1.subscribe(channel);
        //监听订阅成功事件
        client1.on("subscribe", function (channel, count) {
            console.log("client subscribed to " + channel + "," + count + " total subscriptions");
        });
    }

    static msgOn() {
        // return new Promise((resolve, reject) => {
        //收到消息后执行回调，message是redis发布的消息
        client1.on("message", function (channel, message) {
            // resolve(message)
            console.log("我接收到信息了" + message);
        });
        // })

    }

    static test() {
        client.on('ready', function (err) {
            //准备就绪后订阅chat频道
            client1.subscribe("chat");
            console.log("client1订阅chat成功。");
        })
        client.on('connect', function () {
            console.log('redis connect ok');
        })
        //监听订阅成功事件
        client.on("subscribe", function (channel, count) {
            console.log("client subscribed to " + channel + "," + count + " total subscriptions");
        });

        //收到消息后执行回调，message是redis发布的消息
        client1.on("message", function (channel, message) {
            console.log("我接收到信息了" + message);
        });

        //监听取消订阅事件
        client1.on("unsubscribe", function (channel, count) {
            console.log("client unsubscribed from" + channel + ", " + count + " total subscriptions")
        });
    }


    static send(channel: string, message: any) {
        // console.log(channel, message);
        client.publish(channel, message);
    }


}

export default redis;